#!/bin/ksh
#(C) COPYRIGHT International Business Machines Corp. 1996.
# All Rights Reserved.  Licensed Materials - Property of IBM
############################################################################
#
# Note:  The code for progress meter reads the size for the file from
#        the disk directly using 'dosdir -l' command.  This command is
#        used for files that have not been stored using 'doswrite -a'.
#        The files that are stored in the dos format have their name and
#        file size stored in $filename.  Any changes to the size of dos
#        formatted files will require a programmer to change the size of
#        these files in this script.
#
###########################################################################
# oct 4 96 gnc - handled cfg.4 file
# Oct 3 96 gnc - prompt to create directory if does not exist, and
#      echo NetView description
# Sep17 96 gnc - changed default directory path name, and product name
# Jul24 96 dgp - Moved the wait fix for showfilestatus routine for AIX 4.1
# Jun12 96 dgp - Put Mike's changes in for the build script.
# Jun05 96 dgp - Put wait in showfilestatus routine for AIX 4.1 fix.
# June 3 96 gnc - Increased disk space requirement from 10000 to 14000
# Apr 11 96 gnc & A.Choy - Modified to handle an additional diskette for CFG.3 f

# file and modified to work on AIX 4.1.
# Dec 11 95 gnc & dgp - Modified the release name, path & readme size.
# Jun 20 95 dgp - Added lines to create a nv6000 starting script.
# Mar 24 95 jvp - Modified for 2210 V1R2. Install from 4 diskettes
# Aug 18 94 dgp - Modified to work with the 2210 Config pgm.
# Mar 01 94 SLL - Moved Samples to their own diskette
# Dec 08 93 SLL - Updated for V1R3.0.0 changes
# Oct 22 93 Sam - no longer compress samples.doc
# Oct 01 93 Sam - placed escape sequence before each dollar sign for AIX 3.2.4


#       The following lines check to see if the user has 10 meg of disk
#       space available on their hard disk.  A directory for installing
#       software is also created if one has not been created by the user.
#************************************************************************

while true
do
        clear
        done=false
        currentpath=`pwd`
        showpath=`echo $currentpath | sed 's/$/\/masc200/'`
        echo 'Press <ENTER> to accept the default directory path'
        echo '['$showpath']'
        echo 'for the 2216 Configuration Program or type the desired path.'
        echo ': \c'; read installpath

        if [ -z "$installpath" ]; then
               installpath=$showpath
        fi

        if echo "$installpath" | grep '/' > /dev/null; then
                truncpath=`echo $installpath | sed 's/\/[A-Za-z0-9\.\_]*$//g'`
                firstchar=`echo $truncpath | cut -c1`
                if [ "$firstchar" = $ ]; then
                        truncpath=`eval echo \$truncpath`
                        installpath=`eval echo \$installpath`
                fi
        else
                truncpath=$currentpath
                installpath=`echo $currentpath'/'$installpath`
        fi


       index=1
                for argu in `df -I $truncpath | sed '1 d'`; do
                        if [ "$index" -eq 4 ]; then
                                value=$argu
                        fi
                        index=`expr $index + 1`
                done

       if (("$index"  == 1)); then
          echo 'Directory ['$truncpath'] does not exist'
          echo 'Do you want to create directory ['$truncpath']? \c'
          echo ' '
          echo 'Press (y or n) and then press <ENTER> [n] \c'

          read answer
          if [ "$answer" = y ]; then
             mkdir $truncpath
             if ( test $? -eq 0); then
                index=1
                for argu in `df -I $truncpath | sed '1 d'`; do
                   if [ "$index" -eq 4 ]; then
                          value=$argu
                   fi
                  index=`expr $index + 1`
               done
             else
                exit 2
             fi
          else
            exit 0
          fi
        fi

               # if 14 Meg space available
                if [ "$value" -gt 14000 ]; then
                   if [ ! -d "$installpath" ]; then
                      mkdir $installpath
                      done=true; cd $installpath
                   else
                      cd $installpath

                      #checking to see if the older version of
                      if [ -z "$(ls -l | grep cfg.app)" ]; then
                         echo $installpath
                         done=true
                      else
                         echo
                         echo 'Previous version of 2216 Configuration \c'
                         echo 'program found in path'
                         echo '['$installpath']'
                         echo 'Do you wish to overwrite the old version. \c'
                         echo 'Press (y or n) and then press <ENTER> \c'
                         read command
                         if [ "$command" = y ]; then
                            done=true
                         else
                            cd ..
                         fi
                      fi
                  fi
                else
                        echo
                        echo 'Error: Not enough space in ['$installpath'] \c'
                        echo 'file system.'
                        echo '       Please install 2216 Configuration Program
in \c'
                        echo 'another directory. \007'
                        exit 1
                fi

                if [ \( -f /usr/bin/dosread  -a  -f /usr/bin/doswrite \)  -a \( -f /usr/bin/dosdir  -a  -f /usr/bin/csh \) ]
                then
                        if test "$done" = true; then
                                break 3
                        fi
                else
                        if [ \( -f /bin/dosread  -a  -f /bin/doswrite \) -a \(
-f /bin/dosdir  -a  -f /bin/csh \) ]
                        then
                                if test "$done" = true; then
                                        break 4
                                fi
                        fi
                        echo
                        echo 'Error: Could not find the AIX commands, \c'
                        echo 'dosread, doswrite, dosdir or csh'
                        echo 'in the /bin or /usr/bin directories. These \c'
                        echo 'four commands must be installed'
                        echo 'to run the 2216 Configuration Program on AIX \c'
                        echo 'systems. \007'
                        exit 1
                fi
done

#       Disk copy header
###############################################################################
###
header () {
echo '    Filename                                 % Disk copied'
echo
}

#       Shows the file name.
###############################################################################
#
showfilename () {
name=`echo $filename | cut -f1 -d:`
if [ -f "$name" ]; then
        chmod +w "$name"
        rm "$name"
fi

echo '   ' $name '\c'
}

#      Check to see if the file has started to be copied to harddisk.
##############################################################################
fileexist() {
while [ ! -f "$outfile" ]; do
        sleep 1
done
}

#       Shows the progress meter while the file is being copied.
###############################################################################
showcopystatus() {
fname=`echo $filename | cut -f1 -d:`
filesize=`echo $filename | cut -f2 -d:`
tenpercent=`expr $filesize \/ 10`
copied=$tenpercent
echo '* \c'; fileexist
if [ -z "$outfile" ]; then
        outfile = fname
fi
while [ "$copied" -le "$filesize" ]; do
        currfilesize=`ls -l $outfile | cut -c33-41`
        if [ "$currfilesize" -ge "$copied" ]; then
                echo '* \c'
                copied=`expr $copied + $tenpercent`
        fi
done
}

#       Shows the percentage of disk copied.
#############################################################################
showfilestatus () {
fname=`echo $filename | cut -f1 -d:`
length=`echo $fname | wc -c`
length=`expr $length - 1`
length=`expr 15 - $length`
index=1
        while [ "$index" -ne "$length" ]; do
                echo ' \c'
                index=`expr $index + 1`
        done
        showcopystatus
        echo '       ' $percentage
wait
}

#       Gets the file size from the disk
###########################################################################
getfilesize () {
uppcasefilename=`echo $filename | tr '[a-z]' '[A-Z]'`
uppcasefilename=`echo $uppcasefilename '\040'`
size=`dosdir -l | grep "$uppcasefilename" | cut -c13-23`
filename=`echo $outfile:$size`
}

#       Appends .Z to file name and uncompresses the file.
##########################################################################
restorefile () {
# stripping off file size from file name
#newname=`echo $filename | sed 's/\: [0-9]*$//'`
# appending .Z to the file name
cfile=`echo $outfile | sed 's/$/.Z/'`
# changing the actual file name to filename.Z
mv "$outfile" "$cfile"
uncompress "$cfile"
}

#       The following lines prompt user for inserting the disk, and copy
#       the program files from disk to hard disk.
###########################################################################

clear
dosdir | grep INSTALL.AIX > /dev/null
while test $? -ne 0; do
 echo 'Please insert Diskette 1.  Press the <ENTER> key when ready... \007'
 read junk
 dosdir | grep INSTALL.AIX > /dev/null
done
sample=false
#echo 'Copy configuration samples from diskette. \c'
#echo 'Press (y or n) and then press <ENTER> \c'
#read ans
#if [ "$ans" = y ]; then
#   sample=true
#fi
echo 'Copying files from Diskette 1 ...'
echo;  header

### NOTE: In the following section, <<filename>> will be
### replaced with the size of the file by the build script
#filename=cfg.ic$; outfile=cfg.ico; getfilesize; percentage='14%'
#showfilename; dosread CFG.IC$ cfg.ico &
#showfilestatus; restorefile
filename=cfg.ai$; outfile=cfg.aix; getfilesize; percentage='29%'
showfilename; dosread CFG.AI$ cfg.aix &
showfilestatus; restorefile
filename=XCFG:4500; outfile=cfg; percentage='43%'
showfilename; dosread -a XCFG cfg &
showfilestatus
filename=AIX.INI:52; outfile=cfg.ini; percentage='57%'
showfilename; dosread -a AIX.INI cfg.ini &
showfilestatus
filename=cfg.ms$; outfile=cfg.msg; getfilesize; percentage='71%'
showfilename; dosread CFG.MS$ cfg.msg &
showfilestatus; restorefile
filename=cfg.lg$; outfile=cfg.lgo; getfilesize; percentage='86%'
showfilename; dosread CFG.LG$ cfg.lgo &
showfilestatus; restorefile
filename=README:40965; percentage='100%'
showfilename; dosread -a README README &
showfilestatus
echo; echo 'Done reading files from Diskette 1.'

echo; echo 'Please insert Diskette 2.  Press the <ENTER> key when ready... \007'
read junk

dosdir | grep 'CFG.1\$'> /dev/null
while test $? -ne 0; do
 echo 'Please insert Diskette 2.  Press the <ENTER> key when ready...\007'
 read junk
 dosdir | grep 'CFG.1\$'> /dev/null
done

echo 'Copying files from Diskette 2 ...'
echo; header
filename=cfg.1$; outfile=cfg.1; getfilesize; percentage='100%'
showfilename; dosread CFG.1$ cfg.1 &
showfilestatus
echo; echo 'Done reading files from Diskette 2.'

echo; echo 'Please insert Diskette 3.  Press the <ENTER> key when ready... \007'
read junk

dosdir | grep 'CFG.2\$'> /dev/null
while test $? -ne 0; do
 echo 'Please insert Diskette 3.  Press the <ENTER> key when ready...\007'
 read junk
 dosdir | grep 'CFG.2\$'> /dev/null
done

echo 'Copying files from Diskette 3 ...'
echo; header
filename=cfg.2$; outfile=cfg.2; getfilesize; percentage='100%'
showfilename; dosread CFG.2$ cfg.2 &
showfilestatus
echo; echo 'Done reading files from Diskette 3.'

echo; echo 'Please insert Diskette 4.  Press the <ENTER> key when ready...\007';
read junk

dosdir | grep 'CFG.3\$'> /dev/null
while test $? -ne 0; do
 echo 'Please insert Diskette 4.  Press the <ENTER> key when ready... \007'
read junk
 dosdir | grep 'CFG.3\$'> /dev/null
done

echo 'Copying files from Diskette 4 ...'
echo; header

filename=cfg.3$; outfile=cfg.3; getfilesize; percentage='100%'
showfilename; dosread CFG.3$ cfg.3 &
showfilestatus

echo; echo 'Done reading files from Diskette 4.'

echo; echo 'Please insert Diskette 5.  Press the <ENTER> key when ready... \007'
read junk

dosdir | grep 'CFG.4\$'> /dev/null
while test $? -ne 0; do
 echo 'Please insert Diskette 5.  Press the <ENTER> key when ready...\007'
 read junk
 dosdir | grep 'CFG.4\$'> /dev/null
done

echo 'Copying files from Diskette 5'
echo; header

filename=cfg.4$ outfile=cfg.4; getfilesize; percentage='84%'
showfilename; dosread CFG.4$ cfg.4 &
showfilestatus

cat cfg.1 cfg.2 cfg.3 cfg.4 > cfg.app
rm cfg.1 cfg.2 cfg.3 cfg.4; outfile=cfg.app; restorefile

filename=#BATCH.COS:5145; outfile=#BATCH.cos; percentage='86%'
showfilename; dosread -a '#BATCH.COS' '#BATCH.cos' &
showfilestatus
filename=#BATCHSC.COS:5151; outfile=#BATCHSC.cos; percentage='88%'
showfilename; dosread -a '#BATCHSC.COS' '#BATCHSC.cos' &
showfilestatus
filename=#CONNECT.COS:2276; outfile=#CONNECT.cos; percentage='90%'
showfilename; dosread -a '#CONNECT.COS' '#CONNECT.cos' &
showfilestatus
filename=#INTER.COS:5147; outfile=#INTER.cos; percentage='92%'
showfilename; dosread -a '#INTER.COS'  '#INTER.cos' &
showfilestatus
filename=#INTERSC.COS:5152; outfile=#INTERSC.cos; percentage='95%'
showfilename; dosread -a '#INTERSC.COS' '#INTERSC.cos' &
showfilestatus
filename=CPSVCMG.COS:2276; outfile=CPSVCMG.cos;percentage='97%'
showfilename; dosread -a 'CPSVCMG.COS' 'CPSVCMG.cos'
showfilestatus
filename=SNASVCMG.COS:2277; outfile=SNASVCMG.cos; percentage='100%'
showfilename; dosread -a SNASVCMG.COS SNASVCMG.cos
showfilestatus

echo; echo 'Done reading files from Diskette 5.'


echo; echo 'Please insert Diskette 6.  Press the <ENTER> key when ready... \007'
read junk

dosdir | grep 'CFG.HL\$'> /dev/null
while test $? -ne 0; do
 echo 'Please insert Diskette 6. Press the <ENTER> key when ready...\007'
 read junk
 dosdir | grep 'CFG.HL\$'> /dev/null
done

echo 'Copying files from Diskette 6 ...'
echo; header


filename=cfg.hl$; outfile=cfg.hlp; getfilesize; percentage='100%'
showfilename; dosread CFG.HL$ cfg.hlp &
showfilestatus; restorefile

echo; echo 'Done reading files from Diskette 6.\007'


# copy the samples database and doc file if the user wants them
if [ "$sample" = true ]; then
   echo; echo 'Please insert Samples Diskette.  Press the <ENTER> key when
ready...\007'; read junk

   dosdir | grep SAMPLES.DOC > /dev/null
   while test $? -ne 0; do
    echo 'Please insert Samples Diskette.  Press the <ENTER> key when ready...
\007'; read junk
    dosdir | grep SAMPLES.DOC > /dev/null
   done

   echo 'Copying files from Samples Diskette ...'
   echo; header

   filename=samples.cdb; outfile=samples.cdb; getfilesize; percentage='50%'
   showfilename; dosread samples.cdb samples.cdb &
   showfilestatus

   filename=SAMPLES.DOC; outfile=SAMPLES.DOC; getfilesize; percentage='100%'
   showfilename; dosread SAMPLES.DOC SAMPLES.DOC &
   showfilestatus

   echo; echo 'Done reading files from Samples Diskette.\007'
fi

chmod ugo+x cfg.aix
chmod ugo+x cfg

cd $currentpath
chmod u+w INSTALL.AIX
rm INSTALL.AIX
cd $installpath


#       The following lines install a shell script for NV6000 V2 to use to
#       invoke the configuration program from the installation subdirectory.
###########################################################################
echo 'Do you intend to use this config program with NV6000 \c'
echo '(NetView - IBM network observation tool) ? \c'
echo
echo 'Press (y or n) and then press <ENTER> [n]  \c'
read command
if [ "$command" = y ]; then
  echo "cd $installpath" > $installpath/nv6000.cfg
  echo "cfg" >> $installpath/nv6000.cfg
  chmod ugo+x $installpath/nv6000.cfg
fi

echo; echo 'Installation of Version 2 of the IBM Nways Multiaccess Connector'
echo 'Configuration Program is complete. \c'
echo;
echo 'To start the configuration program, type cfg and then press <ENTER>. \007'
